﻿using Godot;
using System;
using System.Collections.Generic;
using File = System.IO.File;
using Path = System.IO.Path;
public class ProjectData
{
    private readonly string _path;
    public string Name { get; }
    public string Description { get; set; }
    public string Author { get; set; }
    public string Version { get; set; }
    public DateTime CreateDateTime { get; set; }
    public DateTime UpdateDateTime { get; set; }
    public List<string> Resources { get; set; }
    public ProjectData(string path)
    {
        _path = path;
        if (File.Exists(path))
        {
            var data = Load();
            Name = (string)data[nameof(Name)];
            Description = (string)data[nameof(Description)];
            Author = (string)data[nameof(Author)];
            Version = (string)data[nameof(Version)];
            CreateDateTime = (DateTime)data[nameof(CreateDateTime)];
            UpdateDateTime = (DateTime)data[nameof(UpdateDateTime)];
            Resources = new List<string>((string[])data[nameof(UpdateDateTime)]);
        }
        else
        {
            Name = Path.GetFileNameWithoutExtension(path);
            Description = "这是一个UI项目";
            Author = string.Empty;
            Version = "1.0.0";
            UpdateDateTime = CreateDateTime = DateTime.Now;
            Resources = new List<string>();
            Save();
        }
    }
    public Dictionary<string, object> Load()
    {
        var result = JSON.Parse(File.ReadAllText(_path));
        return (Dictionary<string, object>)result.Result;
    }
    public void Save()
    {
        var data = new Dictionary<string, object>()
            {
                {nameof(Name), Name},
                {nameof(Description), Description},
                {nameof(Author), Author},
                {nameof(Version), Version},
                {nameof(CreateDateTime), CreateDateTime},
                {nameof(UpdateDateTime), UpdateDateTime},
                {nameof(Resources), Resources},
            };
        File.WriteAllText(Path.Combine(_path), JSON.Print(data));
    }
}
